#!/usr/bin/env python
# encoding: utf-8
'''
@author: Excelsiorly
@license: (C) Copyright 2022, All Rights Reserved.
@contact: excelsiorly@qq.com
@file: 144. 二叉树的前序遍历.py
@time: 2022/1/15 14:46
@desc: https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
> 给你二叉树的根节点 root ，返回它节点值的 前序 遍历。
1. Ot(N), Os(n)
'''

# Definition for a binary tree node.
class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution(object):
    def preorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        def preOrder(root):
            if not root: return
            res.append(root.val)
            preOrder(root.left)
            preOrder(root.right)
        res = []
        preOrder(root)
        return res